﻿
using SHKF.B2C.Model.Entity.Center;
using SHKF.B2C.Model.Req;
using SqlSugar;
using System;
using System.Collections.Generic;

namespace SHKF.B2C.BLL.Center
{
    public class ProductBaseBLL : CenterBLL
    {
        public ProductBaseBLL(SqlSugarClient db) : base(db)
        {

        }

        public List<ProductBaseEntity> List(string Upc, string ProductName, string LiceseId, string CommonTitle)
        {
            try
            {
                var cmdText = this.db
               .Queryable<ProductBaseEntity>();

                if (!string.IsNullOrEmpty(Upc))
                {
                    cmdText.Where(ww => ww.Upc.Contains(Upc));
                }

                if (!string.IsNullOrEmpty(ProductName))
                {
                    cmdText.Where(ww => ww.Upc.Contains(ProductName));
                }

                if (!string.IsNullOrEmpty(LiceseId))
                {
                    cmdText.Where(ww => ww.Upc.Contains(LiceseId));
                }

                if (!string.IsNullOrEmpty(CommonTitle))
                {
                    cmdText.Where(ww => ww.Upc.Contains(CommonTitle));
                }

             
                List<ProductBaseEntity> data = cmdText.ToList();
                return data;

            }
            catch (System.Exception ex)
            {
                throw ex;
            }

        }

        public List<ProductBaseEntity> PageList(ProductBasePageReq req, ref int totalNumber, ref int totalPage)
        {
            try
            {
                var cmdText = this.db
               .Queryable<ProductBaseEntity>();

                if (!string.IsNullOrEmpty(req.Upc))
                {
                    cmdText.Where(ww => ww.Upc.Contains(req.Upc));
                }

                if (!string.IsNullOrEmpty(req.ProductName))
                {
                    cmdText.Where(ww => ww.Upc.Contains(req.ProductName));
                }

                if (!string.IsNullOrEmpty(req.LiceseId))
                {
                    cmdText.Where(ww => ww.Upc.Contains(req.LiceseId));
                }

                if (!string.IsNullOrEmpty(req.CommonTitle))
                {
                    cmdText.Where(ww => ww.Upc.Contains(req.CommonTitle));
                }
 
                List<ProductBaseEntity> data = cmdText.ToPageList(req.PageIndex, req.PageSize, ref totalNumber, ref totalPage);
                return data;

            }
            catch (System.Exception ex)
            {
                throw ex;
            }

        }


        public bool IsExist(string upc)
        {
            bool isExist = this.db
                       .Queryable<ProductBaseEntity>()
                       .Where(ww => ww.Upc == upc)
                       .Any();

            return isExist;
        }

        public ProductBaseEntity Get(string upc)
        {
            return this.db
                        .Queryable<ProductBaseEntity>()
                        .Where(ww => ww.Upc == upc)
                        .First();
        }

        public ProductBaseEntity Save(ProductBaseEntity data)
        {
            try
            {

                if (data.SN == 0)
                {
                    data.pDate = DateTime.Now;
                    data.SN = this.db.Insertable<ProductBaseEntity>(data).ExecuteReturnBigIdentity();
                }
                else
                {
                    this.db.Updateable<ProductBaseEntity>(data).ExecuteCommand();
                }

                return data;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public int Delete(List<long> snList)
        {
            int result = 0;
            try
            {
                this.db.BeginTran();

                foreach (long sn in snList)
                {
                    result += this.db.Deleteable<ProductBaseEntity>().Where(ww => ww.SN == sn).ExecuteCommand();
                }

                this.db.CommitTran();

                return result;
            }
            catch (System.Exception ex)
            {
                this.db.RollbackTran();
                throw ex;
            }
        }
    }
}
